//
//  HelloWorldLayer.m
//  ShootTheBird
//
//  Created by iD Student on 7/2/12.
//  Copyright __MyCompanyName__ 2012. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"

// HelloWorldLayer implementation
@implementation HelloWorldLayer

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
    self=[super init];
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if(self) {
        [self setColor:ccWHITE];
        CCSprite *background=[CCSprite spriteWithFile:@"white.png"];
        [self addChild:background];
        CCLabelTTF *nameP = [[CCLabelTTF alloc] initWithString:@"Points earned:" dimensions:CGSizeMake(150, 50) alignment:UITextAlignmentCenter fontName:@"Papyrus" fontSize:20];
        nameP.color = ccc3(0, 0, 0);
        nameP.position = ccp(80, 290);
        [self addChild:nameP];
        point = [[CCLabelTTF alloc] initWithString:@"0" dimensions:CGSizeMake(100, 50) alignment:UITextAlignmentCenter fontName:@"Papyrus" fontSize:20];
        point.color = ccc3(0, 0, 0);
        point.position = ccp(180, 290);
        [self addChild:point];
        CCLabelTTF *nameA = [[CCLabelTTF alloc] initWithString:@"Arrows used:" dimensions:CGSizeMake(150, 50) alignment:UITextAlignmentCenter fontName:@"Papyrus" fontSize:20];
        nameA.color = ccc3(0, 0, 0);
        nameA.position = ccp(330, 290);
        [self addChild:nameA];
        count = [[CCLabelTTF alloc] initWithString:@"0" dimensions:CGSizeMake(100, 50) alignment:UITextAlignmentCenter fontName:@"Papyrus" fontSize:20];
        count.color = ccc3(0, 0, 0);
        count.position = ccp(430, 290);
        [self addChild:count];
        angryBirdsObj=[[NSMutableArray alloc]init];
        birdsObj = [[NSMutableArray alloc] init];
        arrows = [[NSMutableArray alloc] init];
        angryBirds = [[NSMutableArray alloc] initWithObjects:@"ab.png",@"ab1.png",@"ab2.png",@"ab3.png",@"ab4.png",@"ab5.png", nil];
        birds=[[NSMutableArray alloc] initWithObjects:@"Bird 1.png",@"bird.png",@"bird3.png", nil];
        deleteBird = [[NSMutableArray alloc] init];
        deleteArrow = [[NSMutableArray alloc] init];
        deleteAngryBird = [[NSMutableArray alloc] init];
        self.opacity = 1.0;
        shooter=[CCSprite spriteWithFile:@"bow&arrow.png"];

		// ask director the the window size
		size = [[CCDirector sharedDirector] winSize];
        self.isTouchEnabled = YES;
		shooter.position =  ccp( size.width /2 , 20 );
        [shooter runAction:[CCRotateTo actionWithDuration:0.001 angle:-angle-55]];
        [shooter setScale:0.5];
        [self addChild: shooter];
        [NSTimer scheduledTimerWithTimeInterval:arc4random()%20/10+0.5 target:self selector:@selector(birdsFly) userInfo:nil repeats:YES];
        [NSTimer scheduledTimerWithTimeInterval:arc4random()%20/10+0.5 target:self selector:@selector(angryBirdsFly) userInfo:nil repeats:YES];
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(shoot) userInfo:nil repeats:YES];
	}
	return self;
}


// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint tapPosition;
    UITouch *touch = [touches anyObject];
    shoot=[CCSprite spriteWithFile:@"Arrow.png"];    
    CGPoint location = [touch locationInView:[touch view]];
    shoot.position=ccp(size.width/2,20);
    tapPosition = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:location]];
    float dY = shooter.position.y - tapPosition.y;
    float dX = shooter.position.x - tapPosition.x;
    angle = CC_RADIANS_TO_DEGREES(atan2f(dY, dX));    
    [shooter runAction:[CCRotateTo actionWithDuration:0.07 angle:-angle-47]];
    [shoot setRotation: -angle-18];
    float slope=dY/dX;
    float x=320/slope;
    [shoot runAction:[CCMoveTo actionWithDuration:0.7 position:CGPointMake(x+240, 380)]];
    [self addChild:shoot];
    [arrows addObject:shoot];
    count.string = [NSString stringWithFormat:@"%i", count.string.intValue +1];

}
-(void)birdsFly
{
    yValue=arc4random()%100+200;
    bird=[CCSprite spriteWithFile:[birds objectAtIndex:arc4random()%3]];
    bird.position = CGPointMake(0, yValue);
    id moveBirdAcross = [CCMoveTo actionWithDuration:arc4random()%25/10+2.0 position:CGPointMake(700, yValue)];
    [bird runAction:moveBirdAcross];
    [self addChild:bird];
    [birdsObj addObject:bird];
    


}
-(void)angryBirdsFly{
    xValue=arc4random()%50+175;
    angryBird=[CCSprite spriteWithFile:[angryBirds objectAtIndex:arc4random()%6]];
    angryBird.position = CGPointMake(0, xValue);
    id moveAngryBirdAcross = [CCMoveTo actionWithDuration:arc4random()%25/10+2.0 position:CGPointMake(700, xValue)];
    [angryBird runAction:moveAngryBirdAcross];
    [self addChild: angryBird];
    [angryBirdsObj addObject:angryBird];


}
-(void)shoot
{ 
                
    for(CCSprite *arrow in arrows){
        for(CCSprite *aBird in birdsObj){
            CGRect arrowRect=[self rectForSprite:arrow];
            CGRect birdRect=[self rectForSprite:aBird];
            if (CGRectIntersectsRect(arrowRect,birdRect)) {
                point.string = [NSString stringWithFormat:@"%i", point.string.intValue -2];
                [deleteBird addObject:aBird];
                [deleteArrow addObject:arrow];

            }
        }

        for(CCSprite *aAngryBird in angryBirdsObj){
            CGRect arrowRect=[self rectForSprite:arrow];
            CGRect angryBirdRect=[self rectForSprite:aAngryBird];
            if (CGRectIntersectsRect(arrowRect,angryBirdRect)) {
                point.string = [NSString stringWithFormat:@"%i", point.string.intValue + 1];
                [deleteAngryBird addObject:aAngryBird];
                [deleteArrow addObject:arrow];
            }
        }
        if(arrow.position.x>430||arrow.position.y>300){
            [self removeChild:arrow cleanup:YES];
            [deleteArrow addObject:arrow];

        }
    }
    
    [self deleteSprite];
}
-(void)deleteSprite{
    for(CCSprite *theBird in deleteBird){
        [CCMoveTo actionWithDuration:1 position:CGPointMake(theBird.position.x, -10)];
        [birdsObj removeObject:theBird];
        [self removeChild:theBird cleanup:YES];
    }
    for(CCSprite *theBird in deleteAngryBird){
        [angryBirdsObj removeObject:theBird];
        [self removeChild:theBird cleanup:YES];
    }
    for(CCSprite *theBird in deleteArrow){
        [arrows removeObject:theBird];
        [self removeChild:theBird cleanup:YES];
    }
}
-(CGRect)rectForSprite:(CCSprite *)sprite
{
    CGRect spriteRect = CGRectMake(sprite.position.x, sprite.position.y, sprite.textureRect.size.width, sprite.textureRect.size.height);
    return spriteRect;
}
@end
